var rootview = ui("$");
var app = sm("do_App");
var page = sm("do_Page");
var nf = sm("do_Notification");
var dialog = sm("do_Dialog");
var openHelper = require("helper/open");
var httpSilence = require("helper/httpSilence");
var http = require("helper/http");
var pubUrl = require("url/pub");
var bizzType = require("helper/bizzType");
var voteUrl = require("url/vote");
var voteHelper = require("helper/vote");
var circleUrl = require("url/circle");
var deviceHelper = require("helper/device");
var common = require("common");

var voteItemUrl=voteUrl.url.item.list;

var voteResultUrl=voteUrl.url.result.create;

//图片展示gridView
var do_GridView_1 = ui("do_GridView_1");
var detailDescribe = ui("detailDescribe");

var do_LinearLayout_7 = ui("do_LinearLayout_7");
var do_GridView_2 = ui("do_GridView_2");
var listdataVoteItem = mm("do_ListData");
do_GridView_2.bindItems(listdataVoteItem);

var buttonAddVoteResult=ui("buttonAddVoteResult");
var aLayoutToResult=ui("aLayoutToResult");
var labelMultiMaxNum=ui("labelMultiMaxNum");
var aLayoutIsSupportMulti=ui("aLayoutIsSupportMulti");

var do_LinearLayout_parise=ui("do_LinearLayout_parise");
rootview.setMapping({
	"aLayoutToResult.tag":"bizzType",
	"do_LinearLayout_7.tag":"bizzId",
    "UserName.text" : "creatorName",
    "UserPic.tag" : "creatorId",
    "UserPic.source" : "fullCreatorImg",
    "detailDescribe.text" : "content",
    "InforTime.text" : "cnNewAt",
    "imgConcern.tag" : "isConcern",
    "do_GridView_1.tag" : "bigImgItems",
    "do_GridView_1.items" : "imgItems",
    "do_GridView_1.visible" : "hasImg",
    "do_ALayout_5.tag" : "id",
    "do_ALayout_6.visible" : "isShowPraiseList",
    //"praiseList.text" :"praiseUserList",
    "do_LinearLayout_parise.tag" : "userList4Praise",//点赞人列表
    "do_LinearLayout_7.visible":"isVote",
    "do_LinearLayout_7.height":"voteHeight"
    
});

rootview.on("dataRefreshed", function(){
	if(do_LinearLayout_7.visible){
		loadVoteItemData();
	}
	
	if(ui("imgConcern").tag == 1){
		ui("imgConcern").source = "source://image/common/concern1.png";
	}else{
		ui("imgConcern").source = "source://image/common/concern0.png";
	}
});
page.on("showAllPraise", function(data, e) {
	showAllPraise(data.userList4Praise);
});
function showAllPraise(userList4Praise){
	var userList = userList4Praise;//点赞人列表
	var keyIds = do_LinearLayout_parise.getChildren();
	for(var i in keyIds){
		ui(keyIds[i]).remove();
	}
	if (userList.length>0) {
		var y=0;
		var i=0
		var keyrow=do_LinearLayout_parise.add("keyrow"+y, "source://view/public/praise/detailPraiseRow.ui", "");
		var keyrowll=ui(keyrow+".do_LinearLayout_row");
		var label;
		do {
			var keyl=keyrowll.add("key"+i, "source://view/public/praise/detailPraise.ui", "key"+(i-1));
		
			label=ui(keyl+".do_Label_key");
			label.text=" "+userList[i].nick;
			ui(keyl+".do_ALayout_root").tag=userList[i].id;
			
			if (keyrowll.getRect().width>do_LinearLayout_parise.width){
				y+=1;
				keyrow = do_LinearLayout_parise.add("keyrow" + y, "source://view/public/praise/detailPraiseRow.ui", "keyrow" + (y - 1));
				keyrowll = ui(keyrow + ".do_LinearLayout_row");
				// 删掉添加的label
				ui(keyl).remove();
				continue;
			}
			
			//点击搜索
			ui(keyl+".do_ALayout_root").on("touch", "", 2000, function() {
				openHelper.openPersonal(this.tag);
			});	
			i+=1;
		} while (i < userList.length);
	}
	page.fire("refreshList");
}
//点赞后--重新加载点赞人列表
page.on("onPraiseBack", function(data, e) {
	var topicId = ui("do_ALayout_5").tag;
	httpSilence.post(circleUrl.url.topic.getPraiseUserList, {"id":topicId}, function(data) {
		page.fire("praise2updateTopic",data);
	});
});

//图片浏览注册事件
do_GridView_1.on("touch", function(data){
	openHelper.open("source://view/reuse/imgBrowser.ui", {
		imgDatas :do_GridView_1.tag,index:data
	});
});

//点击个人头像-他的人个主页
ui("alayoutUserInfo").on("touch", function(data, e) {
	var userId = ui("UserPic").tag;
	if(userId !=null){
		openHelper.openPersonal(userId);
	}
	
});

//关注按钮
ui("do_ALayout_5").on("touch", function(data, e) {
	var topicId = ui("do_ALayout_5").tag;
	var addParams = {
            bizzId:topicId,
            bizzType:bizzType.Level2.CIRCLE_TOPIC
		};
	if(ui("imgConcern").tag == 1){
		//如果已关注，再次点击则取消关注
		ui("imgConcern").source = "source://image/common/concern0.png";
		ui("imgConcern").tag = 2;	
		httpSilence.post(pubUrl.url.concern.doCancle,addParams,function(data){
			nf.toast("取消关注");
			page.fire("concern2updateTopic",{isConcern:2});
		});
	}else{
		ui("imgConcern").source = "source://image/common/concern1.png";
		ui("imgConcern").tag = 1;
		httpSilence.post(pubUrl.url.concern.doAdd,addParams,function(data){
			nf.toast("已关注");
			page.fire("concern2updateTopic",{isConcern:1});
		});
	}
});
var isSupportMulti,multiMaxNum,isResult;
function loadVoteItemData(){
	var params = {
			voteId : do_LinearLayout_7.tag
	};
	httpSilence.post(voteItemUrl, params, function(data) {
		listdataVoteItem.removeAll();
		isSupportMulti=data.voteBaseVo.isSupportMulti;
		multiMaxNum=data.voteBaseVo.multiMaxNum;
		isResult=data.isResult;
		aLayoutIsSupportMulti.visible=false;
		if(isResult){
			aLayoutToResult.visible=false;
			forData(data,2);
		}else{
			aLayoutToResult.visible=true;
			if(isSupportMulti==2){//单选
				forData(data,0);
			}else{//多选
				aLayoutIsSupportMulti.visible=true;
				labelMultiMaxNum.text="最多选"+multiMaxNum+"项";
				voteHelper.multiCheck(0);
				forData(data,1);
			}
		}
		listdataVoteItem.addData(data.list);
		do_GridView_2.bindItems(listdataVoteItem);
		do_GridView_2.refreshItems();
		do_LinearLayout_7.redraw();
	});
	
}
function forData(data,template){
	for (var i = 0; i < data.list.length; i++) {
		var vitem=data.list[i];
		vitem.template=template;
		vitem.multiMaxNum=multiMaxNum;
		vitem.isCheck=2;
	}
}
var voteItemId;
var voteItemIds=[];
function addVoteResult(){
	var params = {
			voteId : do_LinearLayout_7.tag
	};
	if(isSupportMulti==2){//单选
		params.voteItemId=voteItemId;
	}else{
		params.voteItemIds=voteItemIds.join(";");
	}
	http.post(voteResultUrl, params, function(data) {
		listdataVoteItem.removeAll();
		forData(data,2);
		listdataVoteItem.addData(data.list);
		do_GridView_2.bindItems(listdataVoteItem);
		do_GridView_2.refreshItems();
		do_LinearLayout_7.redraw();
		aLayoutToResult.visible=false;
		nf.toast("投票成功。");
	});
	
}
buttonAddVoteResult.on("touch", function(index) {
	var voteItemIds2=[];
	var alldata = listdataVoteItem.getRange(0);
	for(var i = 0;i<alldata.length;i++){
		if(alldata[i].checked){
			voteItemIds2.push(alldata[i].id);
		}
	}
	if(null!=voteItemId&&''!=voteItemId||voteItemIds2.length>0){
		nf.confirm("您确实投票这个结果吗？", "投票", "确定", "取消", function(data, e) {
			if(data == 1){
				voteItemIds=voteItemIds2;
				addVoteResult();
			}
		})
	}else{
		nf.toast("请选择投票选项。");
	}
})
page.on("check_radio_change_event", function(b) {
	for (var i = 0; i < listdataVoteItem.getCount(); i++) {
		var d = listdataVoteItem.getOne(i);
		if (b.id == d.id&&b.isCheck==1){
			voteItemId=d.id;
			d.source = "source://image/common/radiocheck.png";
		}else{
			d.source = "source://image/common/radio.png";
		}
		listdataVoteItem.updateOne(i, d);
	}
	do_GridView_2.refreshItems();
})
page.on("check_change_event", function(d) {
	var alldata = listdataVoteItem.getRange(0);
	for(var i = 0;i<alldata.length;i++){
		if(alldata[i].id==d.id){
			alldata[i].checked=d.checked;
			listdataVoteItem.updateOne(i, alldata[i]);
			break;
		}
	}
})
/*复制文字*/
var descLayout = ui("descLayout");
descLayout.on("touch","",300,function(){
	var datas = {
		"text":	detailDescribe.text
	}
	var osType = deviceHelper.getOsType();
	var dialogHref = osType==="2"?"source://view/reuse/copyTextIos.ui":"source://view/reuse/copyText.ui";
	dialog.open(dialogHref, datas, function(data, e) {  
	});
});
